package com.zllkj.mm.module.home.fragments

import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.services.core.LatLonPoint
import com.amap.api.services.core.PoiItem
import com.amap.api.services.poisearch.PoiResult
import com.amap.api.services.poisearch.PoiSearch
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener
import com.orhanobut.logger.Logger
import com.zllkj.mm.R
import com.zllkj.mm.adapter.CollectionListAdapter
import com.zllkj.mm.adapter.PoiSearchAdapter
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.module.goods.vm.ItemPostImageVM
import com.zllkj.mm.module.home.fragments.vm.ItemSearchPoiResultVM
import com.zllkj.mm.util.JsonUtil
import com.zllkj.mm.util.common.AppUtils
import kotlinx.android.synthetic.main.fragment_poi_search_list.view.*

/**
 * This fragment is used to search some of location that is near to you\
 *
 * When your location has been changed , you should start this page agagin
 */
class PoiNearSearchResultFragment :BaseTitleFragment() {
    lateinit var adapter : PoiSearchAdapter
    var datas = ArrayList<ItemSearchPoiResultVM>()
    override fun getContentLayoutId(): Int {
        return R.layout.fragment_poi_search_list
    }

    override fun loadData() {
        super.loadData()

        setCommonTitle("地址列表")

        adapter = PoiSearchAdapter(R.layout.item_poi_search_list,datas)
        mRootView.rv_mycollection.layoutManager = LinearLayoutManager(requireContext())
        mRootView.rv_mycollection.adapter = adapter
        mRootView.smartRefreshlayout_mycollection.setEnableRefresh(false)
        mRootView.smartRefreshlayout_mycollection.setEnableLoadMore(false)

        adapter.setOnItemClickListener { baseQuickAdapter, view, i ->
            AppUtils.putFragmentData("location",datas[i])
            removeFragment()
        }
        startPoiSearch()

    }
    private fun startPoiSearch() {
        val query = PoiSearch.Query("", "", "")
        query.pageSize = 30
        query.pageNum = 0
        val poisearch = PoiSearch(context, query)
        poisearch.setOnPoiSearchListener(object : OnPoiSearchListener {
            override fun onPoiSearched(poiResult: PoiResult, i: Int) {
                poiResult.pois.map {
                    var vm = ItemSearchPoiResultVM()
                    vm.city = it.cityName
                    vm.adName = it.adName
                    vm.distance = it.distance
                    vm.provinceName = it.provinceName
                    vm.businessArea = it.businessArea
                    vm.detailAddress = it.title
                    datas.add(vm)
                }
                adapter.notifyDataSetChanged()

            }

            override fun onPoiItemSearched(poiItem: PoiItem, i: Int) {}
        })
        poisearch.bound = PoiSearch.SearchBound(LatLonPoint(AppUtils.lat,AppUtils.lng),10000)
        poisearch.searchPOIAsyn()
    }

}